package com.ecy.quadratic.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ecy.quadratic.R;
import com.ecy.quadratic.beans.Comment;
import com.ecy.quadratic.reader.utils.FormatUtils;
import com.ecy.quadratic.reader.utils.ScreenUtils;
import com.ecy.quadratic.widget.ScrollGridView;
import com.yuyh.easyadapter.glide.GlideCircleTransform;

import java.util.ArrayList;
import java.util.List;

public class CommunityCommentListViewAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context mContext;
    private List<Comment> list;
    private int wh;

    public CommunityCommentListViewAdapter(Context context, List<Comment> list) {
        super();
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.wh = (ScreenUtils.getScreenWidth() - ScreenUtils.dpToPxInt(36)) / 3;
        this.list = list;
    }

    public List<Comment> getList() {
        return list;
    }

    public void setList(List<Comment> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int arg0) {
        return list == null ? null : list.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return list == null ? null : arg0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (list.size() == 0) {
            return null;
        }
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_comment, null);
            holder = new ViewHolder();
            holder.userPhotoIV = (ImageView) convertView.findViewById(R.id.community_user_portrait_iv);
            holder.nicknameTV = (TextView) convertView.findViewById(R.id.community_user_nickname_tv);
            holder.timeTV = (TextView) convertView.findViewById(R.id.community_item_time_tv);
            holder.contentTV = (TextView) convertView.findViewById(R.id.community_item_title_tv);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final Comment bean = list.get(position);

        String name = null, time = null, content = null, headpath = null;

        if (bean != null) {
            if (bean.userBean != null) {
                name = bean.userBean.nickname;
                headpath = bean.userBean.portrait;
            }
            time = bean.createtime;
            content = bean.content;
        }

        if (!TextUtils.isEmpty(name.trim())) {
            holder.nicknameTV.setText(name);
        }

        if (!TextUtils.isEmpty(time)) {
            holder.timeTV.setText(FormatUtils.getDescriptionTimeFromDateString(time));
        }

        if (!TextUtils.isEmpty(content)) {
            holder.contentTV.setText(content);
            Linkify.addLinks(holder.contentTV, Linkify.WEB_URLS);
        }

        if (!TextUtils.isEmpty(headpath)) {
            Glide.with(mContext).load(headpath).placeholder(R.drawable.cover_default_square).transform(new GlideCircleTransform(mContext)).into(holder.userPhotoIV);
        }
//		convertView.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				CommunityDetailActivity.startActivity(mContext,bean);
//			}
//		});
        return convertView;
    }

    private void initInfoImages(ScrollGridView imageGridView, final CommunityGridViewAdapter adapter, final ArrayList<String> imgspaths) {
        int itemWidth = wh;

        int gridViewWidth = 0;
        switch (imgspaths.size()) {
            case 1:
                gridViewWidth = 2 * wh + ScreenUtils.dpToPxInt(5);
                imageGridView.setNumColumns(1);
                itemWidth = gridViewWidth;
                break;
            case 2:
            case 4:
                gridViewWidth = 2 * wh + ScreenUtils.dpToPxInt(5);
                imageGridView.setNumColumns(2);
                break;
            case 3:
            case 5:
            case 6:
                gridViewWidth = wh * 3 + ScreenUtils.dpToPxInt(5) * 2;
                imageGridView.setNumColumns(3);
                break;
            default:
                gridViewWidth = wh * 3 + ScreenUtils.dpToPxInt(5) * 2;
                imageGridView.setNumColumns(3);
                break;

        }
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(gridViewWidth, RelativeLayout.LayoutParams.WRAP_CONTENT);
        imageGridView.setLayoutParams(lp);
        adapter.updateData(imgspaths, itemWidth, 0);
//			imageGridView.setOnItemClickListener(new OnItemClickListener() {
//				@Override
//				public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
//					CommunityDetailActivity.startActivity(mContext,adapter.getItem(arg2));
////					Toast.makeText(mContext, "选择了"+adapter.getItem(arg2)+"图片", Toast.LENGTH_LONG).show();
////					Toast.makeText(mContext, "选择了"+(arg2+1)+"图片", Toast.LENGTH_LONG).show();
//				}
//			});


    }

    class ViewHolder {
        ImageView userPhotoIV;
        TextView nicknameTV;
        TextView timeTV;
        TextView contentTV;
        ScrollGridView imagesGV;
        CommunityGridViewAdapter mAdapter;

        public void bindAdapter() {
            mAdapter = new CommunityGridViewAdapter(mContext);
            imagesGV.setAdapter(mAdapter);
        }
    }


}
